package LeetcodeTest_thirty_one;
//179.最大数

public class largestNumber
{
    public static void main(String[] args)
    {
        int[] nums={0,9,8,7,6,5,4,3,2};
        System.out.println(solution.largestNumber(nums));
    }
}
class solution
{
    public static String largestNumber(int[] nums)
    {
        int i,j,n;
        int maxIndex,temp,sum;
        String str1,str2;
        String answer=new String();
        n=nums.length;
        sum=0;
        for(Integer value:nums)
            sum+=value;
        if(sum==0)
            return "0";
        for(i=0;i<n-1;i++)
        {
            maxIndex=i;
            for(j=i+1;j<n;j++)
            {
                str1=Integer.toString(nums[maxIndex]);
                str2=Integer.toString(nums[j]);
                if((str1+str2).compareTo(str2+str1)<0)
                {
                    maxIndex=j;
                }
            }
            temp=nums[i];
            nums[i]=nums[maxIndex];
            nums[maxIndex]=temp;
        }
        for(i=0;i<n;i++)
            answer=answer+Integer.toString(nums[i]);
        return answer;
    }
}